*********************************************************************
***      IDENTIFY OBSERVATION STAYS TO MATCH HCUP DEF 			  ***
*** Identify observation stays in a way that matches HCUP SEDD    ***
*** definition: observation services + ED services. 			  ***
*** Within that, vary the definition of obs stay as well. 		  ***
*** Last edited: 12/1/2022                       				  ***
*********************************************************************


log using "${CodePath}/log/identify_obs_$S_DATE.log", text replace

*--------------------------------------------------
* Identifying observation stays and whether they originated in the ED in outpatient revenue files
*--------------------------------------------------
forval year = 2007/2015{
	di "Outpatient - `year'"
	*preserve
	use clm_id rev_cntr hcpcs_cd rev_dt bene_id rev_chrg using "${McareDataPath}/harm/100pct/op/`year'/opr`year'.dta", clear

	di "creating dummy for ED charges in claim..."
	gen temp_ED = rev_cntr == "0450" | rev_cntr == "0451" | rev_cntr == "0452" | rev_cntr == "0453" | rev_cntr == "0454" | rev_cntr == "0455" | rev_cntr == "0456" | rev_cntr == "0457" | rev_cntr == "0458" | rev_cntr == "0459" | ///
		hcpcs_cd == "99281" | hcpcs_cd == "99282" | hcpcs_cd == "99283" | hcpcs_cd == "99284" | hcpcs_cd == "99285" 
	bys clm_id: egen any_ED = max(temp_ED)
	drop temp_ED

	di "restricting to just obs..."
	keep if rev_cntr == "0760" | rev_cntr == "0762" | hcpcs_cd == "G0378" | hcpcs_cd == "G0379" ///
		| hcpcs_cd == "99217" | hcpcs_cd == "99218" | hcpcs_cd == "99219" | hcpcs_cd == "99220" | hcpcs_cd == "99234" | hcpcs_cd == "99235"| hcpcs_cd == "99236"

	// count each claim only once
	bys clm_id: gen n = _n
	keep if n == 1

	gen file_year = `year' 

	di "merging in bsfab..."
	merge m:1 bene_id using "${McareDataPath}/harm/100pct/bsfab/`year'/bsfab`year'.dta", keepusing(hmoind*)
	tab _merge 
	keep if _merge == 3 | _merge == 1
	drop _merge 	

	di "merging in claim info..."
	preserve
		use clm_id thru_dt provider pmt_amt using "${McareDataPath}/harm/100pct/op/`year'/opc`year'.dta", clear
		rename provider prvdrnum
		keep if clm_id != ""
		tempfile temp
		save `temp'
	restore

	merge m:1 clm_id using `temp'
	tab _merge 
	keep if _merge == 3 | _merge == 1
	drop _merge

	di "keeping only ffs patient"
	// Keep only if FFS patient in that month
	gen disc_mon = month(thru_dt)
	keep if (disc_mon == 1 & hmoind01 == "0" | disc_mon == 2 & hmoind02 == "0" | disc_mon == 3 & hmoind03 == "0" | disc_mon == 4 & hmoind04 == "0" | disc_mon == 5 & hmoind05 == "0" | disc_mon == 6 & hmoind06 == "0" | disc_mon == 7 & hmoind07 == "0" | disc_mon == 8 & hmoind08 == "0" | disc_mon == 9 & hmoind09 == "0" | disc_mon == 10 & hmoind10 == "0" | disc_mon == 11 & hmoind11 == "0" |  disc_mon == 12 & hmoind12 == "0" )
	drop disc_mon hmoind* thru_dt

	save "${DataPath}/observation/simp_opobsED_`year'.dta", replace 
		
}   


*--------------------------------------------------
* Merging them together
*--------------------------------------------------

use "${DataPath}/observation/simp_opobsED_2007.dta", clear
gen year = 2007
forval year = 2008/2014{
	append using "${DataPath}/observation/simp_opobsED_`year'.dta"
	replace year = `year' if missing(year)
}

gen obs = 1
bys clm_id: gen clm_pmt_amt = pmt_amt if _n == 1
replace clm_pmt_amt = 0 if missing(clm_pmt_amt)

*--------------------------------------------------
* Collapse all 
*--------------------------------------------------

preserve
	gcollapse (sum) obs = obs obs_clm_pmt_amt = clm_pmt_amt, by(prvdrnum year)

	save "${DataPath}/observation/opobs_claimlevel_0715_year.dta", replace
restore


*--------------------------------------------------
* Restrict to ED and collapse
*--------------------------------------------------

// restrict to observation stays with ED
keep if any_ED == 1

preserve
	gcollapse (sum) obs = obs obs_clm_pmt_amt = clm_pmt_amt, by(prvdrnum year)

	save "${DataPath}/observation/opobsED_claimlevel_0715_year.dta", replace
restore



log close



